"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : range_list二级分区表:default/unique
Create At   : 2022/2/17
Owner       : @daiguatutu
Description :
    1.创建二级分区表,二级分区键包含列约束default
    2.插入并查询数据，不指定col_2的值
    3.创建二级分区表，二级分区键包含列约束unique index_parameters
    4.查看索引
    5.创建二级分区表，一级分区键包含列约束unique index_parameters
    6.查看索引
    7.清理环境
Expect      :
    1.成功
    2.成功,col_2的值为20
    3.成功
    4.成功
    5.成功
    6.成功
    7.清理成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0196'
        self.t_name_01 = 't_subpartition_0196_01'
        self.i_name_01 = 'i_subpartition_0196_01'
        self.i_name_02 = 'i_subpartition_0196_02'

    def test_subpartition(self):
        text = '-----step1:创建二级分区表,二级分区键包含列约束default expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name} (col_1 int , " \
            f"col_2 int default 20 , col_3 int , col_4 int )" \
            f"partition by range (col_1) subpartition by list (col_2) " \
            f"( partition p_range_1 values less than(-10) " \
            f"  ( subpartition p_list_1_1 values('-1','-2','-3','-4','-5')," \
            f"    subpartition p_list_1_2 values(default)), " \
            f"  partition p_range_2 values less than(20) " \
            f"  ( subpartition p_list_2_1 values('6','7','8','9','10'), " \
            f"    subpartition p_list_2_2 values(default)), " \
            f"  partition p_range_3 values less than(30) " \
            f"  ( subpartition p_list_3_1 values(default)), " \
            f"  partition p_range_4 values less than(40) " \
            f"  ( subpartition p_list_4_1 values(default)), " \
            f"  partition p_range_5 values less than(maxvalue) " \
            f") enable row movement; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step2:插入并查询数据，不指定col_2的值 expect:成功,col_2的值为20-----'
        self.log.info(text)
        sql = f"insert into {self.t_name}(col_1,col_3,col_4) " \
            f"values(1,1,1),(5,5,5);" \
            f"select * from {self.t_name};" \
            f"select * from {self.t_name} subpartition(p_list_2_2);"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertEqual(4, msg.count('20'), '执行失败:' + text)

        text = '-----step3:创建二级分区表，二级分区键包含列约束unique index_parameters' \
               ' expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name} (col_1 int , " \
            f"col_2 int unique , col_3 int , col_4 int )" \
            f"partition by range (col_1) subpartition by list (col_2) " \
            f"( partition p_range_1 values less than(-10) " \
            f"  ( subpartition p_list_1_1 values('-1','-2','-3','-4','-5')," \
            f"    subpartition p_list_1_2 values(default)), " \
            f"  partition p_range_2 values less than(20) " \
            f"  ( subpartition p_list_2_1 values('6','7','8','9','10'), " \
            f"    subpartition p_list_2_2 values(default)), " \
            f"  partition p_range_3 values less than(30) " \
            f"  ( subpartition p_list_3_1 values(default)), " \
            f"  partition p_range_4 values less than(40) " \
            f"  ( subpartition p_list_4_1 values(default)), " \
            f"  partition p_range_5 values less than(maxvalue) " \
            f") enable row movement; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step4:查看索引 expect:成功-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\di')
        self.log.info(msg)
        self.assertIn(f'{self.t_name}_col_2_tableoid_key', msg,
                      '执行失败:' + text)

        text = '-----step5:创建二级分区表，一级分区键包含列约束unique index_parameters' \
               ' expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name} (col_1 int unique , " \
            f"col_2 int , col_3 int , col_4 int )" \
            f"partition by range (col_1) subpartition by list (col_2) " \
            f"( partition p_range_1 values less than(-10) " \
            f"  ( subpartition p_list_1_1 values('-1','-2','-3','-4','-5')," \
            f"    subpartition p_list_1_2 values(default)), " \
            f"  partition p_range_2 values less than(20) " \
            f"  ( subpartition p_list_2_1 values('6','7','8','9','10'), " \
            f"    subpartition p_list_2_2 values(default)), " \
            f"  partition p_range_3 values less than(30) " \
            f"  ( subpartition p_list_3_1 values(default)), " \
            f"  partition p_range_4 values less than(40) " \
            f"  ( subpartition p_list_4_1 values(default)), " \
            f"  partition p_range_5 values less than(maxvalue) " \
            f") enable row movement; "
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step6:查看索引 expect:成功-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\di')
        self.log.info(msg)
        self.assertIn(f'{self.t_name}_col_1_tableoid_key', msg,
                      '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step7:清理环境-----')
        text_1 = '-----删除表 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};' \
            f'drop table if exists {self.t_name_01};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)

        self.assertEqual(2, msg.count(self.constant.TABLE_DROP_SUCCESS),
                         '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
